package org.study.neo4j.boot.controller;

import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.study.neo4j.boot.dto.TeacherDTO;
import org.study.neo4j.boot.model.Teacher;
import org.study.neo4j.boot.service.TeacherService;
import org.study.neo4j.boot.util.ConvertToDtoUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 作用描述
 *
 * @author doveylovey
 * @version v1.0.0
 * @email 1135782208@qq.com
 * @date 2025年04月09日
 */
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@RestController
@RequestMapping("/api/teachers")
public class TeacherController {
    private final TeacherService teacherService;

    @GetMapping
    public List<TeacherDTO> getAllTeachers() {
        return teacherService.getAllTeachers().stream()
                .map(ConvertToDtoUtils::convertToTeacherDTO)
                .collect(Collectors.toList());
    }

    @GetMapping("/{id}")
    public TeacherDTO getTeacherById(@PathVariable("id") Long id) {
        return ConvertToDtoUtils.convertToTeacherDTO(teacherService.getTeacherById(id));
    }

    @PostMapping
    public TeacherDTO createTeacher(@Validated @RequestBody Teacher teacher) {
        return ConvertToDtoUtils.convertToTeacherDTO(teacherService.saveTeacher(teacher));
    }

    @GetMapping("/by-course/{courseCode}")
    public TeacherDTO getTeacherByCourse(@PathVariable("courseCode") String courseCode) {
        return ConvertToDtoUtils.convertToTeacherDTO(teacherService.getTeacherByCourseCode(courseCode));
    }

    @GetMapping("/advisors/{studentName}")
    public List<TeacherDTO> getAdvisorsByStudent(@PathVariable("studentName") String studentName) {
        return teacherService.getAdvisorsByStudentName(studentName).stream()
                .map(ConvertToDtoUtils::convertToTeacherDTO)
                .collect(Collectors.toList());
    }

    @DeleteMapping("/{id}")
    public void deleteTeacher(@PathVariable("id") Long id) {
        teacherService.deleteTeacher(id);
    }
}
